package com.lance.answer.gui.answer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.dictionary.CoreSynonymDictionary;
import com.hankcs.hanlp.dictionary.common.CommonSynonymDictionary;
import com.lance.answer.util.MD5Utils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.openqa.selenium.json.Json;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p><p>
 *
 * @author : fengx
 * @since JDK8.0
 * create by fengx on 2021/7/15 9:34
 */
public class DealJsonString {

    public static double getSimilarity(String sentence1, String sentence2) {
        List<String> sent1Words = getSplitWords(sentence1);
     //   System.out.println(sent1Words);
        List<String> sent2Words = getSplitWords(sentence2);
       // System.out.println(sent2Words);
        List<String> allWords = mergeList(sent1Words, sent2Words);

        int[] statistic1 = statistic(allWords, sent1Words);
        int[] statistic2 = statistic(allWords, sent2Words);

        double dividend = 0;
        double divisor1 = 0;
        double divisor2 = 0;
        for (int i = 0; i < statistic1.length; i++) {
            dividend += statistic1[i] * statistic2[i];
            divisor1 += Math.pow(statistic1[i], 2);
            divisor2 += Math.pow(statistic2[i], 2);
        }
        return dividend / (Math.sqrt(divisor1) * Math.sqrt(divisor2));
    }
    private static int[] statistic(List<String> allWords, List<String> sentWords) {
        int[] result = new int[allWords.size()];
        for (int i = 0; i < allWords.size(); i++) {
            result[i] = Collections.frequency(sentWords, allWords.get(i));
        }
        return result;
    }
    private static List<String> mergeList(List<String> list1, List<String> list2) {
        List<String> result = new ArrayList<>();
        result.addAll(list1);
        result.addAll(list2);
        return result.stream().distinct().collect(Collectors.toList());
    }
    private static List<String> getSplitWords(String sentence) {
        // 去除掉html标签
        sentence = Jsoup.parse(sentence.replace(" ","")).body().text();
        // 标点符号会被单独分为一个Term，去除之
        return HanLP.segment(sentence).stream().map(a -> a.word).filter(s -> !"`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？ ".contains(s)).collect(Collectors.toList());
    }


    public static void main(String[] args) throws Exception {

        System.err.println(delHTMLTag("<p><em><span style=\"font-family: 'times new roman', times, serif;\">AB</span></em></p>"));
        System.err.println(delHTMLTag("<p><span>三阶行列式<img alt=\"\" height=\"76\" src=\"/api/uploads/20000164436/in-rich-content?created_at=1649408312\" width=\"73\"></span><span>的余子式<em><span style=\"font-family: 'times new roman', times, serif;\">M<sub>23</sub>=</span></em></span><span>（</span><span>  </span><span>　）．</span></p>"));


        System.err.println(saveZhWord("非齐次线性方程组相容的充分必要条件是．（   ）|[http://oss.ouchn.cn/zongbu/jiaoyu/%E5%B7%A5%E7%A8%8B%E6%95%B0%E5%AD%A6%EF%BC%88%E6%9C%AC%EF%BC%89/xingkao2/2221.png, http://oss.ouchn.cn/zongbu/jiaoyu/%E5%B7%A5%E7%A8%8B%E6%95%B0%E5%AD%A6%EF%BC%88%E6%9C%AC%EF%BC%89/xingkao2/2222.png]" ));

        System.err.println(removeSpace("试题正文\n选择题\nJim Thorpe was a Native American. He was born in 1888 in an Indian Territory(印第安人保护区)that is now Oklahoma. Like most Native American children then, he liked to fish, hunt, swim, and play games outdoors. He was healthy and strong, but he had very little formal education. In 1950, Jim Thorpe was named the greatest American football player. He was also an Olympic gold medal winner. But Thorpe had many tragedies in his life.\nJim had a twin brother who died when he was nine years old. By the time he was 16, his mother and father were also dead, Jim then went to a special school in Pennsylvania for Native American children. There, he learned to read and write and also began to play sports. Jim was poor, so he left school for two years to earn some money. During this time, he played on a baseball team. The team paid him only $15 a week. Soon he returned to school to complete his education. Jim was a star athlete (运动员) in several sports, including baseball, running, and football. He won many awards for his athletic ability, mainly for football. In many games, he scored all or most of the points for his team.\nIn 1912, when Jim Thorpe was 24 years old, he became part of the U.S Olympic team. He competed in two very difficult events: the pentathlon and the decathlon. Both require great ability and strength. The pentathlon has five track and field events, including the long jump and the 1500-meter race. The decathlon has ten track and field events, with running, jumping and throwing contests.\nPeople thought it was impossible for an athlete to compete in both the pentathlon and the decathlon. So everyone was surprised when Thorpe won gold medals in both events. When the King of Sweden presented Thorpe with his two gold medals, he said, “Sir, you are the greatest athlete in the world.” Thorpe was a simple and honest man. He just answered, “Thanks, King.”\n1. From the passage we learn that Jim Thorpe was born in\n回答\nA\nB\nC\n\n\n.\nA. India\nB. Pennsylvania\nC. Oklahoma\n2. According to the passage, most American Indian children loved all the following EXCEPT\n回答\nA\nB\nC\n\n\n.\nA. fishing\nB. singing\nC. swimming\n3. Jim Thorpe started to play sports\n回答\nA\nB\nC\n\n\n.\nA. before he was 9 years old\nB. when he was 16 years old\nC. when he was 24 years old\n4. The word decathlon in paragraph 3 probably means\n回答\nA\nB\nC\n\n\n.\nA. jumping\nB. five track and field events\nC. ten track and field events\n5. Which of the following is NOT TRUE?\n回答\nA\nB\nC\n\n\n\nA. In 1912, Thorpe went back to finish his college education.\nB. Thorpe won two gold medals in the 1912 Olympic Games.\nC. Thorpe once played on a baseball team for money."));        Thread.sleep(2000000000);

     //   String regex = "/<\\/?.+?\\/?>/g";
        String sss= "<span class=\"questionBody\" data-v-4daf4b1e=\"\">人民法院应当将开庭的时间、地点在开庭(&nbsp; &nbsp;)日以前通知人民检察院。</span>" ;
        System.err.println(delHTMLTag(sss));
        System.err.println(delHTMLTag("大学生职业生涯决策困难原因之一是缺乏对自己的了解，对自己的了解包括（  ）"));
        System.err.println(delHTMLTag("大学生职业生涯决策困难原因之一是缺乏对自己的了解。对自己的了解包括（ ）"));

        String s1 = delHTMLTag("大学生职业生涯决策困难原因之一是缺乏对自己的了解，对自己的了解包括（  ）");
        String s2 = delHTMLTag("大学生职生涯决策困难原因之一是缺乏对自己的了解。对自己的了解包括（ ）");
        System.out.println(HanLP.segment("你好，欢迎使用HanLP汉语处理包！"));
        System.out.println( CoreSynonymDictionary.similarity("12222","12222"));
        System.out.println( getSimilarity(s1,s2));
        System.out.println( CoreSynonymDictionary.get("大")==null);





    }

    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式

    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
    private static final String regEx_space = "\\s*|\t|\r|\n";// 定义空格回车换行符
    private static final String regEx_w = "<w[^>]*?>[\\s\\S]*?<\\/w[^>]*?>";//定义所有w标签


    /**
     * @param htmlStr
     * @return 删除Html标签
     * @author LongJin
     */
    public static String delHTMLTag(String htmlStr) {
        Pattern p_w = Pattern.compile(regEx_w, Pattern.CASE_INSENSITIVE);
        Matcher m_w = p_w.matcher(htmlStr);
        htmlStr = m_w.replaceAll(""); // 过滤script标签


        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签


        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签


        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签


        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签


        htmlStr = htmlStr.replaceAll("&nbsp;", ""); //过滤&nbsp;
        return htmlStr.trim(); // 返回文本字符串
    }

    //[\u4e00-\u9fa5]

    // 只保留汉字
    public static String saveZhWord(String htmlStr) {
        Pattern p_w = Pattern.compile("[^\\u4e00-\\u9fa5]", Pattern.CASE_INSENSITIVE);
        Matcher m_w = p_w.matcher(htmlStr);
        htmlStr = m_w.replaceAll(""); // 过滤script标签
        return htmlStr.trim(); // 返回文本字符串
    }


    public static String removeSpace(String news) {
        String answer = news.replaceAll("[\\pP‘’“”]", "").replaceAll("\r", "").replaceAll("\n", "")
                .replaceAll("\\s", "").replaceAll("", "");

        return answer;
    }

}
